<template>
  <div class="msg-set">
    <el-dialog
      :title="visible.type===0? '新增直播分类':'修改直播分类'"
      :visible.sync="visible.show"
      width="600px"
      center
      :close-on-click-modal="false"
    >
      <div class="content">
        <div class="line flex-left">
          <label class="title">分类名称</label>
          <el-input v-model="param.name" size="medium" placeholder="请输入分类名称"></el-input>
        </div>
        <div class="line flex-left">
          <label class="title">排序</label>
          <el-input v-model.number="param.sort" size="medium" placeholder="请输入分类序号"></el-input>
        </div>
        <div class="line img-upload flex-left">
          <label class="title">logo</label>
          <el-upload
            ref="my-upload"
            :class="{'scaleIMGdisabled':disabledUpload===true}"
            :action="url"
            :data="{type:'application'}"
            :headers="headers"
            list-type="picture-card"
            :on-remove="remove"
            :on-error="error"
            :on-success="success"
            :before-upload="()=>{disabledUpload=true;onUpload=true}"
            :file-list="imgList"
          >
            <i class="el-icon-plus"></i>
            <!-- <div slot="tip" class="el-upload__tip">提示：建议图片尺寸1005*420，使用深色底，图片大小进行压缩。</div> -->
          </el-upload>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible.show = false">取 消</el-button>
        <el-button
          size="small"
          type="primary"
          @click="submit"
          :disabled="onSubmit"
          :loading="onSubmit"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addCategory, updateCategory } from "./live.service.js";
export default {
  props: ["visible", "id"],
  data() {
    return {
      param: {
        name: "",
        sort: "",
        logo: "",
        platform_id: ""
      },
      url: "/root/api/oss/upload",
      imgList: [],
      disabledUpload: false,
      headers: {
        token: this.$store.state.token
      },
      onSubmit: false,
      onUpload: false
    };
  },
  watch: {
    visible: {
      handler(item) {
        this.param.platform_id = Number(this.id);
        if (item.type === 1) {
          this.disabledUpload = true;
          if (this.imgList.length === 0)
            this.imgList = [{ url: item.data.logo }];
          this.param = JSON.parse(JSON.stringify(item.data));
        }
        if (item.show === false) {
          this.param = {
            name: "",
            sort: "",
            logo: "",
            platform_id: ""
          };
          this.disabledUpload = false;
          this.imgList = [];
        }
      },
      deep: true
    }
  },
  methods: {
    async submit() {
      if (this.param.title === "")
        this.$notify.error({
          title: "提示",
          message: "标题不能为空"
        });
      if (this.param.content === "")
        this.$notify.error({
          title: "提示",
          message: "内容不能为空"
        });
      const res =
        this.visible.type === 0
          ? await addCategory(this.param)
          : await updateCategory(this.param);
      if (res.code === 0) {
        this.$notify.success({
          title: "提示",
          message: this.visible.type === 0 ? "添加成功" : "修改成功"
        });
        this.$emit("success");
      }
      this.visible.show = false;
    },
    remove() {
      this.param.logo = "";
      this.disabledUpload = false;
    },
    error() {
      this.disabledUpload = false;
      this.onUpload = false;
      this.$notify.error({
        title: "提示",
        message: this.visible.type === 0 ? "图片上传失败！" : "图片修改失败！",
        duration: 1500
      });
    },
    success(res) {
      this.param.logo = res.data || "";
      this.onUpload = false;
      this.$notify.success({
        title: "提示",
        message: this.visible.type === 0 ? "图片上传成功" : "图片修改成功",
        duration: 1500
      });
    }
  }
};
</script>

<style lang="scss">
.msg-set {
  .el-dialog__header {
    border-bottom: 1px solid #e9e9e9;
    padding: 15px;
    font-weight: 600;
  }
  .content {
    h3 {
      font-size: 16px;
      text-align: center;
      margin-bottom: 20px;
      font-weight: 600;
    }
    .txt {
      text-indent: 2em;
      // line-height: 18px;
    }
    .limit-text input {
      padding-right: 60px;
    }
    .line {
      // line-height: 50px;
      margin-top: 10px;
      .title {
        width: 4em;
        text-align: right;
        margin-right: 20px;
        flex-shrink: 0;
      }
    }
    .el-upload__tip {
      line-height: 20px;
      color: #d93a49;
    }
  }
  .scaleIMGdisabled {
    .el-upload--picture-card {
      display: none;
    }
  }
}
</style>